﻿
CREATE PROCEDURE [dbo].[USP_ClearTableData]
(
	@TableName VARCHAR(128) = ''
)
AS 
	IF @TableName <> '' 
	BEGIN
		DECLARE @Sql VARCHAR(255)
		SET @Sql = 'delete from ' + @TableName
		PRINT @Sql
		EXEC( @Sql )
		IF ( IDENT_SEED(@TableName) IS NOT NULL ) 
		BEGIN
			DBCC checkident( @TableName, reseed, 0 )
			PRINT '有种子且成功重置为1'
		END
	END
	ELSE 
	begin	 
		BEGIN TRANSACTION
		DECLARE @cTblName VARCHAR(128)
		DECLARE cur_Clear CURSOR
		FOR
			SELECT  RTRIM(name)
			FROM    sysobjects
			WHERE   type = 'U'
			ORDER BY crdate DESC
		OPEN cur_Clear
		DECLARE @cSQL VARCHAR(255)
		FETCH NEXT FROM cur_Clear INTO @cTblName
		WHILE ( @@fetch_status = 0 ) 
			BEGIN
				SET @cSQL = 'delete from ' + @cTblName
				PRINT @cSQL
				EXEC( @cSQL )
				IF ( IDENT_SEED(@cTblName) IS NOT NULL ) 
					BEGIN
						DBCC checkident( @cTblName, reseed, 0 )
						PRINT '有种子且成功重置为1'
					END
				FETCH NEXT FROM cur_Clear INTO @cTblName
			END
		CLOSE cur_Clear
		DEALLOCATE cur_Clear
		COMMIT
    END

